<?php
/**
 * Created by PhpStorm.
 * User: Machenike
 * Date: 2019/12/14
 * Time: 9:48
 */

namespace App\Service\Alipay;


class AlipayUserCertify
{
//    private $rsaPrivateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCV/mHDGBILcJFXsZQfs+OzVSZQTGKqVjX5GCQWr+H9VQXSA1L1zqqNs+PvoN0G6yepEB1CM1KiPskhvfS0faNiwAydFd6ZrPy/pbGQjbmvt97QM7Jd5w9D94czz1zXkh+mHw3LXj94VC9z9Qpe6I6vd1Poelm2DjWQGEtC/YNz/hr58/WLA40Z/hyILAzarhA0Xvy3/WwWG1Fxv7ERlE1nJZTTJdO4I94+nTSGKvaCIwWarXImloyj/WMy+4ZTmm2ppRDfxe7vxtKJuXcyUwcMXgIDr0sgjUu//QOj+ABDONwKZHMsozw8sOx8xn3toqXsmx3iNnr5G3zNmyv5i7k5AgMBAAECggEAGDUa8BKlbuU2/9EwF8G/oQTADzuUvOr3AQGtaV/veCRNsSxKKlO1YPNEeTscEO7I0tc/8Zw43OiHK4Ptx2dzhA98ual66JBrpj6Gns7QxhRFyJJfWhpdc1myn2D7GeuCYhwm7MiEM9ZaZjuI9fJQfO8kQqPkcKZwbJYPJ3ukyIZd4snHVtkXe+1c87GFiYlVs/oAQwL+Rfq56IFoVWE5aN5EHrfUFDBpYpnPuY/aPfy1NYccrVGCmc7xoEzrz4+CPgKfC64UxBq8HIHCUL98UiyWQagST5spmy0ntY/cobyeQv3NvFGgaGiKcT1gjuACLaIVHGeBm1BMOkR/QEunkQKBgQDMmdjTstjZl1RaLfx8WQZD93+xlD0joyDSYvcyJxhTtUfkeBFk9O0YSWaKjkQasZGc9wL/QIew1031/BD6c4VRb4kqRaKbOdRNLDrBJ7Fb1D7MktHvW5Bi2Q//d+ZEtrEdAl8Rqeb1zplPAt9T36fzR7snOS73Fxh2QyTCijKCJQKBgQC7rKtuHliuBAjI7yu8GHI0nqlQZDkTWO+CuycQBVYstqRTtI+ufWY47Is/RT34veJXIm+OjYRqsGcGB30F5cM15PC4cFXmxDcbvEdi4+9cOjSkgTGvIk4VQkBTzvHaGno9n/gAxDijnM5hC6LqWFGQEON6ralW2DYz6J7kRcTshQKBgFKkshiQ3VbG8aObf6karJU3m7S3FBsZAWD56K+xavok920pspz88eglfe3pmUjPAOpOPD+uaSDdVvhNYO5JS0ihrN4OUqgwq7igbPsodVOKX/NyOqpcae4Dj5q5a6HFybTWCGCyKECRuwtXNa5DYSu7hbVKO9ZC1favGGIcQFqhAoGAAz0gnD7j0uT47dbKz20wk13lmCCSp5XSVFQ8gVdGPHFOdq9YB2/cksDqSkY8oo1MWaqbeD9S+LBgpsb8Tly/uK9hxB0ULm5WcSEXrko2uEW84NXl8JrL9Cv3hjq9C8GHOIcJNaItRA2RtjUIM7kgdzcDZfuXP4ttrAmorFHT6y0CgYAmKqyz4BHz4BvoZCFgCu1nyrgJ1tVOpNGDDEdpRS70/c9hkETp1VnZGQCiIVGha0eQX+z906spGhAYkLEywKplUe+W3kChjX9f7SaI1ozFSeShVKAIugaUbD4yRZ8qgNGmbIUh99SeqjDECM75eIIAtDUaI9FIQO0B+GEf4XzNCQ==";
//    //支付宝公钥
//    private $alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmVwOIEmLqkORsZSZXgBdYwQazBvzqIocC24Nm0H/7QpQOFoQ0IDOGYXa8apQgF3U+oTvZxTAcnjWgdLFMpJhHe409hYGGFv2+uquCDSK2iPbNx8Qkxwoi5+tOJpPDoNbkxbQMrDucpiFrVyRbD5x0ZlpzawWG3pUGoX7i9knsvGSv/pPe/tS91Yee8mSTss2fwuFgUnMGlJBW7bEoubvTKjBBAhKEtaOyjENqUCabTb+MeYXFjbpl2FQ7OjXV5X0yhRc7sdkZh308wvKXnlaIFloo4n9jCrI+462x3Pw/LilxITwrX6PfsaPZKTh6D5F95FR2P+82UFRnxbogP+u2QIDAQAB';
//    private $gatewayUrl = "https://openapi.alipay.com/gateway.do";
//    private $appId = "2018041002531952";

    private $rsaPrivateKey = "MIIEowIBAAKCAQEAslHKme2Mx2nr3TN+CkOSIlY2K1QbzZ3x+b70ksQEvCCjdgOG8wpYCy1CntPSyJwr1yfiFV4B8EJqwpApPNos28E4B8kcuSkPXrqNuQPA0SCFFKDnjnZhPBZm6XQYi5M6tg1UxOhU+EsRrC2chDuaDfpvUMHnX9eFBgQpqP0LA5/rdDPsbY7XQ+r8gmW504u+eDE+naech33WSWdQrgEcvgEX4rRwpLkdU/fZwvm/5EoUtKntJipCRsps2MnuQYiURst502yY3YBHx+dm6s8hKVrRJ9CSgrtQt8TGuuwIjIzxbU5MDOXB2yDbkGXXfqhby6m5wXcaBShtntGgOBJv/QIDAQABAoIBAFL9eAJdqWhhppFDVsSmIAv0/a76rRnsCWMHeUIZEm8P7aOODq9sP/+rp9bNlMsiy9CN0k3Je7N65b75YwfcLoZ0DYSqF8EhF/Mpda4GziVGMkmgfUQzWhuh/XFnQ8kp02hJGguuaWVJYsc0TyTHrHIW/Nmj9PAJGPLs2wq4AflEGa2N/3C5VXVccwHvZ5dLW/4MoAI0rqqN2MChLHvcPX+pwKIyv0oKtdj2uIrMiiMsa8cp+JIi00+JGitGll0m22+kZqRnofRBWT35/vha5iecU4SUxK1sze7pMRmRCTCqrDc4iMy1fzmgLEHPQE4hEjT2ppoGFyR3Rwr4H3Wg7+ECgYEA/DTxIaUnWGk+o5sdehotcKqYVtlYq1xr1WOcXkFq6pEnC8Mt52saMt24YkP/oVh0MrnPdkOREYOPh3I06PGGhUvVKgdIfPFXPpOy1AMhcP+dkRqQTDOZeH4A5bGXUaJpjBHgv0EAlIHqA22ZkeEwmf2vhBeAkWSKBvxJ5dQW/QUCgYEAtQBdf8z0jLBOJ/OGPq1vB3K6SNOroyS0OVzkdhRAPRICH0smZ63qn4fLExFAoX+z4eZCNu2mHLmf88q0zOEKRT8Q0KAzVM3O3S3VPH6QRAvfUSZAfZ5TepO6/72dcX9JN249NqwQ2xG57gReUgrNbFyjIlh3y6RmikUr7azF2JkCgYABN1AiVWWHK5q9tzXBLPcjH+90G8Ats1hhwwUx/ybA0spZaCuEfiO2MxN8hc9pu79zryms7E56wyOiB47xwJhhdCgm97oFwDQnfRbTpuA2Z0UfOf2QIH+Q8SqX8O9F2X6gykVHJs0V6jpyocinx2owk+SQwnxANzfjKLxCe29Q8QKBgFdexMNTGNzsod1xFOUebNDolXlgC6LlrcVezL0ey19vWOzoJrK3ZeD5+JHMqykJ7rfloSLIln2ZMNfKinm3S3Dfn2tFWWHfMrLbU+ouoc7m+DZAw7KfgWZS91pJmj1WFu6V3SCN8Fj+9oQpQapSopRjJzAGClWXlxO5jhndY2dJAoGBAPUivfEyykIUEEDrUDiginFP8pBwyhVUIzF6/uMoNgkst3N0FQ1mcyZMjpoHHgGW4ovdWsLCdbH0JtFbDwhzKuZdqayqQyq4ysdhko8OFSD+7S1YMXR2hx8l8t7o7ZTALjrX+kuN2YwfQC4FeZGlETtVqVnvwEAQDw4owwDoAqpa";
    //支付宝公钥
    private $alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1tXUKRoOd+ot9m86nH9uRa2JrYYJ5cM3U0/r9WBSAZBE5y9jRjwH6PDS7L9oxK9acFtcrb1b/nkFE7CpjxbpPBVMzhPtmIVasodJsPXQfzfSjgMZ3nlWAYANgMCIpMwORx9V+2nfjlrJN1yscKe5CyjQmxDIpT9rx4I+58970gz0Pixgn4yUfWlDfXzXc3NL5lbNbnnenIaajDWkTaxDli8bOFZBcTrgW7sRYEhTHzbcObiOoY4UYV2l3rxwdK37LIoZyGoAZAxUHbG4HLy0ylUns5CuVqbqwHbmgOCCS8c9f+lMsaDyi4kfIgaILcfVnYyRJsdZMO9iqB3r9emVTwIDAQAB';
    private $gatewayUrl = "https://openapi.alipay.com/gateway.do";
    private $appId = "2021001102629588";


    public function init($name, $code)
    {
        require(__DIR__ . "/../../../BaseExt/AliPay/aop/AopClient.php");
        require(__DIR__ . "/../../../BaseExt/AliPay/aop/request/AlipayUserCertifyOpenInitializeRequest.php");
        require(__DIR__ . "/../../../BaseExt/AliPay/aop/request/AlipayUserCertifyOpenCertifyRequest.php");
        require(__DIR__ . "/../../../BaseExt/AliPay/aop/request/AlipayUserCertifyOpenQueryRequest.php");
        $aop = new \AopClient ();
        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';
        $request = new \AlipayUserCertifyOpenInitializeRequest ();
        $param["outer_order_no"] = "ZGYD20180913232" . time();
        $param["biz_code"] = "SMART_FACE";
        $param["identity_param"] = [
            "identity_type" => "CERT_INFO"
            , "cert_type" => "IDENTITY_CARD"
            , "cert_name" => "$name"
            , "cert_no" => "$code"
        ];
        $param["merchant_config"] = (["return_url" => ""]);
        $request->setBizContent(json_encode($param));
        try {
            $result = $aop->execute($request);
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage(), 500);
        }

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if (!empty($resultCode) && $resultCode == 10000) {
            $certify_id = $result->$responseNode->certify_id;
            $aop = new \AopClient ();
            $aop->gatewayUrl = $this->gatewayUrl;
            $aop->appId = $this->appId;
            $aop->rsaPrivateKey = $this->rsaPrivateKey;
            $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
            $aop->apiVersion = '1.0';
            $aop->signType = 'RSA2';
            $aop->postCharset = 'UTF-8';
            $aop->format = 'json';
            $request = new \AlipayUserCertifyOpenCertifyRequest();
            $request->setBizContent(json_encode(["certify_id" => $certify_id]));
            try {
                $url = $aop->pageExecute($request, "GET");
            } catch (\Exception $exception) {
                throw new \Exception($exception->getMessage(), 500);
            }
            return [$certify_id,$url];

        } else {
            throw new \Exception("认证初始化失败", 500);
        }
        die;
    }


    public function query($certify_id)
    {
        require(__DIR__ . "/../../../BaseExt/AliPay/aop/AopClient.php");
        require(__DIR__ . "/../../../BaseExt/AliPay/aop/request/AlipayUserCertifyOpenInitializeRequest.php");
        require(__DIR__ . "/../../../BaseExt/AliPay/aop/request/AlipayUserCertifyOpenCertifyRequest.php");
        require(__DIR__ . "/../../../BaseExt/AliPay/aop/request/AlipayUserCertifyOpenQueryRequest.php");
        $aop = new \AopClient ();
        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';

        $aop = new \AopClient ();
        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';
        $request = new \AlipayUserCertifyOpenQueryRequest();
        $request->setBizContent(json_encode(["certify_id" => $certify_id]));
        try {
            $result = $aop->execute($request);
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage(), 500);
        }
//        var_dump($result);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if (!empty($resultCode) && $resultCode == 10000) {
            return $result->$responseNode;
        }else{
            return false;
        }

    }
}
